package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    /*
    * 营业额统计
    * */
    @Override
    public TurnoverReportVO turnover(LocalDate begin, LocalDate end) {
        TurnoverReportVO tr=new TurnoverReportVO();
        ArrayList<LocalDate> datelist=new ArrayList<>();
        datelist.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            datelist.add(begin);
        }
        StringBuilder sb=new StringBuilder();
        StringBuilder sb2=new StringBuilder();
        for (LocalDate localDate : datelist) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Double totalAmout = orderMapper.getTotalAmoutByDate(beginTime,endTime);
            if(totalAmout==null) totalAmout=0.0;
            String s = localDate.toString();
            if(sb.length()==0){
                sb.append(s);
               sb2.append(totalAmout);
            }else{
                sb.append(",").append(s);
                sb2.append(",").append(totalAmout);
            }
            }

        tr.setDateList(sb.toString());
       tr.setTurnoverList(sb2.toString());
        return tr;
    }
/*
* 用户统计
* */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        UserReportVO u=new UserReportVO();
        ArrayList<LocalDate> datelist=new ArrayList<>();
        datelist.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            datelist.add(begin);
        }
        StringBuilder dl=new StringBuilder();
        StringBuilder nl=new StringBuilder();
        StringBuilder tl=new StringBuilder();
        for (LocalDate localDate : datelist) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map=new HashMap();
            map.put("endTime",endTime);
            Long  totaluser=userMapper.getUSerByDate(map);
              map.put("beginTime", beginTime);
            Long  newUser=userMapper.getUSerByDate(map);
            if(totaluser==null) totaluser=0L;
            if(newUser==null) newUser=0L;
            String s = localDate.toString();
            if(dl.length()==0){
                dl.append(s);
                nl.append(newUser);
                tl.append(totaluser);
            }else{
                dl.append(",").append(s);
                nl.append(",").append(newUser);
                tl.append(",").append(totaluser);
            }

        }
        u.setDateList(dl.toString());
        u.setNewUserList(nl.toString());
        u.setTotalUserList(tl.toString());
        return u;
    }
/*
* 订单统计
* */
    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        OrderReportVO or=new OrderReportVO();
        ArrayList<LocalDate> datelist=new ArrayList<>();
        datelist.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            datelist.add(begin);
        }
        StringBuilder dl=new StringBuilder();//日期列表
        StringBuilder ocl=new StringBuilder();//订单数列表
        StringBuilder vocl=new StringBuilder();//有效订单数列表
        Integer validOrderCount = orderMapper.getByStatus(Orders.COMPLETED);//获取有效订单数
        Integer totalCount = orderMapper.getTotalCount();//获取总订单数
        for (LocalDate localDate : datelist) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map=new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
           Integer orderCount = orderMapper.getCountByMap(map);
            map.put("status",Orders.COMPLETED);
            Integer validCount = orderMapper.getCountByMap(map);
            String s = localDate.toString();
            if(dl.length()==0){
                dl.append(s);
                ocl.append(orderCount);
                vocl.append(validCount);
            }else{
                dl.append(",").append(s);
                ocl.append(",").append(orderCount);
                vocl.append(",").append(validCount);
            }

        }
        or.setValidOrderCount(validOrderCount);
        or.setTotalOrderCount(totalCount);
        or.setOrderCompletionRate((double) validOrderCount / totalCount);
        or.setDateList(dl.toString());
        or.setOrderCountList(ocl.toString());
        or.setValidOrderCountList(vocl.toString());
        return or;
    }
/*
* 销量前十统计
* */
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        SalesTop10ReportVO st=new SalesTop10ReportVO();
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
         List<GoodsSalesDTO>  sales10=orderMapper.getSalesTop10(beginTime,endTime);
        List<String> names = sales10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers = sales10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
            StringBuilder namelist=new StringBuilder();
            StringBuilder numberlist=new StringBuilder();

            for (String name : names) {
                if(namelist.length()==0){
                    namelist.append(name);
                }else{
                    namelist.append(",").append(name);
                }
            }
            for (Integer number : numbers) {
                if(numberlist.length()==0){
                    numberlist.append(number);
                }else{
                    numberlist.append(",").append(number);
                }
            }
            st.setNameList(namelist.toString());
            st.setNumberList(numberlist.toString());
        return st;
    }
}
